﻿CREATE FUNCTION dbo.Car_NextList 
( @Year smallint,
	@Make sysname,
	@Model sysname,
	@Series sysname
)RETURNS @TT TABLE (Name sysname NULL)
AS
BEGIN
DECLARE @T TABLE(Name sysname NULL)

IF @Year > 0 AND @Make > '' AND @Model > '' AND @Series > ''
	INSERT INTO @T
	SELECT     Body_Style
	FROM         t_CarInfo_OnLine
	WHERE (Make = @Make) AND (Model = @Model) AND (ISNULL(Series, '') = @Series) AND Body_Style >= ''
	GROUP BY Make, Model, ISNULL(Series, ''), Body_Style
ELSE
IF @Year > 0 AND @Make > '' AND @Model > ''
	INSERT INTO @T
	SELECT  Series
	FROM         t_CarInfo_OnLine
	WHERE (Make = @Make) AND (Model = @Model) AND Series >= ''
	GROUP BY Make, Model,Series
ELSE
IF @Year > 0 AND @Make > ''
	INSERT INTO @T
	SELECT  ISNULL(Model,'')
	FROM         t_CarInfo_OnLine
	WHERE (Make = @Make) AND Model > ''
	GROUP BY Make, Model
ELSE
	INSERT INTO @T
	SELECT  ISNULL(Make,'')
	FROM         t_CarInfo_OnLine
	WHERE Make > ''
	GROUP BY Make
	ORDER BY Make

INSERT INTO @TT
SELECT '' Name UNION ALL
SELECT RTRIM(Name) FROM @T ORDER BY Name

RETURN
END
